let app = getApp()
import Toast from '../../component/vant/toast/toast';

const checkUtil = require('../../utils/checkUtil.js')

Page({

    /**
     * 页面的初始数据
     *
     */
    data: {
        btnDisable: false,
        phone: "",
        password: "",
        code: "",
        loginType: 2,
        canIUseGetUserProfile: false,

        //倒计时时间
        countDownTime: 300000,
        //显示发送验证码
        showCode: true,
        //显示倒计时
        showCountDown: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (wx.getUserProfile) {
            this.setData({
                canIUseGetUserProfile: true
            })
        }
    },

    phoneLogin: function (e) {
        let _this = this
        if (this.data.phone == '') {
            app.toast("请输入手机号", "fail")
            return
        }
        wx.showLoading({
            title: '登录中',
        })
        _this.setData({
            btnDisable: true
        })
        let data = {}
        data.phone = this.data.phone
        data.password = this.data.password
        data.code = this.data.code
        data.loginType = this.data.loginType
        app.formPost('/app/login/phone', data).then(res => {
            wx.hideLoading()
            if (res.code === 0) {
                let data = res.data;
                console.log("登录成功" + data.token)
                wx.setStorageSync('token', data.token)
                app.getUserMenu()
                // 初始化用户信息
                app.initUserData()
                setTimeout(function () {
                    wx.switchTab({
                        url: "/pages/index/index"
                    })
                }, 1000)
            } else {
                app.toast(res.message, "fail")
            }
            _this.setData({
                btnDisable: false
            })
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },
    getPhoneNumber: function (e) {
        if (e.detail.errMsg && e.detail.errMsg.indexOf("fail") > -1) {
            return
        }
        let _this = this
        let data = e.detail
        app.formPost('/app/login/phoneCode', data).then(res => {
            wx.hideLoading()
            if (res.code === 0) {
                let data = res.data;
                if (data.isRegister === 0) {
                    wx.navigateTo({
                        url: "/pages/login/register/index?phone=" + data.phone
                    })
                } else {
                    console.log("登录成功" + data.token)
                    wx.setStorageSync('token', data.token)
                    app.getUserMenu()
                    // 初始化用户信息
                    app.initUserData()
                    app.wxLogin()
                    setTimeout(function () {
                        wx.switchTab({
                            url: "/pages/index/index"
                        })
                    }, 1000)
                }
            } else {
                app.toast(res.message, "fail")
            }
            _this.setData({
                btnDisable: false
            })
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },
    sendSmsCode: function (e) {
        let _this = this
        let phone = this.data.phone
        if (phone == '') {
            this.setData({
                phoneErrorMessage: "请输入手机号"
            })
            return false;
        }
        if (!checkUtil.checkMobile(phone)) {
            this.setData({
                phoneErrorMessage: "手机号格式不正确"
            })
            return false;
        }
        let data = {}
        data.phone = this.data.phone
        app.formPost('/app/login/sendCode', data).then(res => {
            _this.setData({
                spinShow: false
            });
            if (res.code === 0) {
                app.message(res.message, 'success')
                _this.setData({
                    showCode: false,
                    showCountDown: true
                })
                const countDown = this.selectComponent('.control-count-down');
                countDown.start();
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },

    toRegister: function () {
        wx.navigateTo({
            url: "/pages/login/register/index"
        })
    },
    codeLogin: function () {
        this.setData({
            loginType: 1
        })
    },
    passwordLogin: function () {
        this.setData({
            loginType: 2
        })
    }
})
